<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-form-item label="物品名称" prop="name">
        <el-input v-model="dataForm.name" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="库存量" prop="num">
        <el-input v-model="dataForm.num" clearable placeholder="请输入" type="number"></el-input>
      </el-form-item>

      <el-form-item label="库存类型" prop="type">
        <el-select v-model="dataForm.type" clearable placeholder="请选择" style="width: 100%;">
          <el-option label="药品" value="药品"></el-option>
          <el-option label="医疗器械" value="医疗器械"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="过期时间" prop="time">
        <el-date-picker v-model="dataForm.time" type="date" value-format="yyyy-MM-dd" clearable placeholder="选择日期"
          style="width: 100%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="dataForm.brand" clearable placeholder="请输入" type="text"></el-input>
      </el-form-item>
      <el-form-item label="供应商" prop="gys">
        <el-input v-model="dataForm.gys" clearable placeholder="请输入" type="text"></el-input>
      </el-form-item>
      <el-form-item label="经办人" prop="jbr">
        <el-input v-model="dataForm.jbr" clearable placeholder="请输入" type="text"></el-input>
      </el-form-item>
      <el-form-item label="药品批号" prop="drugNo">
        <el-input v-model="dataForm.drugNo" clearable placeholder="请输入" type="text"></el-input>
      </el-form-item>
      <el-form-item label="是否处方" prop="isPres">
        <el-select v-model="dataForm.isPres" clearable placeholder="请选择" style="width: 100%;">
          <el-option label="是" value="是"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="药品类型" prop="drugType">
        <el-select v-model="dataForm.drugType" clearable placeholder="请选择" style="width: 100%;">
          <el-option label="保健" value="保健"></el-option>
          <el-option label="免疫" value="免疫"></el-option>
          <el-option label="皮肤" value="皮肤"></el-option>
          <el-option label="呼吸" value="呼吸"></el-option>
          <el-option label="驱虫" value="驱虫"></el-option>
          <el-option label="清洁" value="清洁"></el-option>
          <el-option label="关节" value="关节"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo } from '@/api/hospital/goods';
export default {
  data() {
    return {
      visible: false,
      dataForm: {},
      dataRule: {
        name: { required: true, message: '物品名称不能为空', trigger: 'blur' },
        num: { required: true, message: '库存量不能为空', trigger: 'blur' },
        type: { required: true, message: '库存类型不能为空', trigger: 'blur' },
        time: { required: true, message: '过期时间不能为空', trigger: 'blur' },
        brand: { required: true, message: '品牌不能为空', trigger: 'blur' },
        gys: { required: true, message: '供应商不能为空', trigger: 'blur' },
        jbr: { required: true, message: '经办人不能为空', trigger: 'blur' },
        drugNo: { required: true, message: '药品批号不能为空', trigger: 'blur' },
        isPres: { required: true, message: '是否处方不能为空', trigger: 'blur' },
        drugType: { required: true, message: '药品类型不能为空', trigger: 'blur' },
      },
    };
  },
  mounted() {
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        this.dataForm = { id };
        if (id) {
          getDataInfo(id)
            .then((data) => {
              if (data && data.code === 0) {
                this.dataForm = data.data;
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          savaDataInfo(this.dataForm)
            .then((res) => {
              if (res && res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  },
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
  },
};
</script>
